package com.wufeng.controller;

import com.wufeng.dto.R;
import com.wufeng.service.oss.AliyunOssService;
import com.wufeng.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *  阿里云OSS对象存储
 */
@RestController
@RequestMapping("/oss")
public class AliyunOssController {

    private final AliyunOssService aliyunOssService;

    @Autowired
    public AliyunOssController(AliyunOssService aliyunOssService) {
        this.aliyunOssService = aliyunOssService;
    }

    /**
     * 处理头像上传
     * @param file
     * @param uploadDir
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFile")
    public R handleFileUpload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "uploadDir", required = false, defaultValue = "imgs/avatar") String uploadDir
    ) throws IOException {
        Map<String, Object> map = new HashMap<>();

        String fileUrl = aliyunOssService.handleFileUpload(file, uploadDir);

        map.put("fileName", StringUtils.getLastSubstringAfterSlash(fileUrl));
        return R.ok(map);
    }

    /**
     * 处理封面上传
     * @param file
     * @param uploadDir
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadCover")
    public R handleCoverUpload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "uploadDir", required = false, defaultValue = "imgs/cover") String uploadDir
    ) throws IOException {
        Map<String, Object> map = new HashMap<>();

        String fileUrl = aliyunOssService.handleFileUpload(file, uploadDir);

        map.put("fileName", StringUtils.getLastSubstringAfterSlash(fileUrl));
        return R.ok(map);
    }

    /**
     * 处理内容文件上传
     * @param file
     * @param uploadDir
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadContentImg")
    public R handleContentImgUpload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "uploadDir", required = false, defaultValue = "imgs/contentImg") String uploadDir
    ) throws IOException {
        Map<String, Object> map = new HashMap<>();

        String fileUrl = aliyunOssService.handleFileUpload(file, uploadDir);

        map.put("fileName", StringUtils.getLastSubstringAfterSlash(fileUrl));
        return R.ok(map);
    }


    /**
     * 处理身份文件上传
     * @param file
     * @param uploadDir
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadIdentityImg")
    public R handleIdentityImgUpload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "uploadDir", required = false, defaultValue = "imgs/identityImg") String uploadDir
    ) throws IOException {
        Map<String, Object> map = new HashMap<>();

        String fileUrl = aliyunOssService.handleFileUpload(file, uploadDir);

        map.put("fileName", StringUtils.getLastSubstringAfterSlash(fileUrl));
        return R.ok(map);
    }
}
